﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using monitor_2_DBwork;

namespace monitor_server_2_WPF.Windows
{
    /// <summary>
    /// Логика взаимодействия для LoginWindow.xaml
    /// </summary>
    public partial class LoginWindow
    {
        private bool _loginEmpty = true;
        private bool _passwordEmpty = true;
        private readonly VoidStringDelegate _del;

        public LoginWindow(VoidStringDelegate d)
        {
            InitializeComponent();

            _del = d;

            var myCommand1 = new RoutedCommand();
            CommandBindings.Add(new CommandBinding(myCommand1, AcceptButton_OnClick));
            myCommand1.InputGestures.Add(new KeyGesture(Key.Enter));

            var myCommand2 = new RoutedCommand();
            CommandBindings.Add(new CommandBinding(myCommand2, CancelButton_OnClick));
            myCommand2.InputGestures.Add(new KeyGesture(Key.Escape));

            LoginLabel.Content = Res.Rm.GetString("Login");
            PasswordLabel.Content = Res.Rm.GetString("Password");
            AcceptButtonContentLabel.Text = Res.Rm.GetString("Accept");
            CancelButtonContentLabel.Text = Res.Rm.GetString("Cancel");
            Title = Res.Rm.GetString("Authorization");
            StatusLabel.Text = Res.Rm.GetString("InputUserNameAndPassword");

            Background = new SolidColorBrush(Res.LighBlack);
            StatusBar.Background = new SolidColorBrush(Res.NiceBlue);
            CheckEnabled();
            LoginBox.Focus();
        }

        private void AcceptButton_OnClick(object sender, RoutedEventArgs e)
        {

            var chk = DBwork.CheckPassword(LoginBox.Text, PasswordBox.Text);
            if (chk == "0")
            {
                StatusLabel.Text = Res.Rm.GetString("WrongUserOrPassword");
                return;
            }
            if (chk != "1")
            {
                StatusLabel.Text = Res.Rm.GetString("InsufficientRights");
                return;
            }
            if(_del != null)
                _del(LoginBox.Text);
            DialogResult = true;
            Close();
        }

        private void CancelButton_OnClick(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }

        private void LoginBox_OnTextChanged(object sender, TextChangedEventArgs e)
        {
            var tb = sender as TextBox;
            _loginEmpty = string.IsNullOrEmpty(tb.Text);
            CheckEnabled();

        }

        private void PasswordBox_OnTextChanged(object sender, TextChangedEventArgs e)
        {
            var tb = sender as TextBox;
            _passwordEmpty = string.IsNullOrEmpty(tb.Text);
            CheckEnabled();
        }

        void CheckEnabled()
        {
            var enabled = _passwordEmpty == false && _loginEmpty == false;
            AcceptButton.IsEnabled = enabled;
        }
    }
}
